<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>web2py shell for {{=app}}</title>
<script type="text/javascript" src="{{=URL(r=request,c='static',f='jquery.js')}}"></script>
<style type="text/css">
* {
  border: 0px;
  background-color: black;
  color: #FF5C1F;
  font-family: monospace;
  font-size: 10pt;
}

table, tr, td {
  text-align: left;
  vertical-align: top;
}

pre {
  border: 0;
  padding: 0;
  margin: 0;
  color: #CCCCCC;
}
dd{
  color: #FFFF00;
}
dt{
  color: #CCCCCC;
}
.prompt, #output {
  width: 45em;
  border: 1px solid #333333;
  font-size: 10pt;
  margin: 0.5em;
  padding: 0.5em;
  padding-right: 0em;
  overflow: auto;
  wrap: hard;
}

#output {
  height:250px;overflow:auto;
}

#toolbar {
  margin-left: 0.5em;
  padding-left: 0.5em;
}

#caret {
  width: 2.5em;
  margin-right: 0px;
  padding-right: 0px;
  border-right: 0px;
}

#statement {
  width: 43em;
  margin-left: -1em;
  padding-left: 0px;
  border-left: 0px;
  background-position: top right;
  background-repeat: no-repeat;
}

.processing {
  background-image: url("{{=URL(r=request,c='static',f='spinner.gif')}}");
}

#ajax-status {
  font-weight: bold;
}

.message {
  color: #8AD;
  font-weight: bold;
  font-style: italic;
}

.error {
  color: #F44;
}

.username {
  font-weight: bold;
}

</style>
</head>

<body>

<div id="output" rows="22" readonly="readonly">
web2py Shell {{=request.env.web2py_version}}
</div>

<form id="form" action="{{=URL(r=request,f='callback',args=app)}}" method="get">
  <textarea class="prompt" id="caret" readonly="readonly" rows="4">&gt;&gt;&gt;</textarea>
  <textarea class="prompt" name="statement" id="statement" rows="4"></textarea>
</form>
<i>Using the shell may lock the database to other users of this app</i><br/>
<a href="{{=URL(r=request,f='unlock',args=app)}}">unlock and commit</a>
<a href="{{=URL(r=request,f='unlock',args=app,vars=dict(rollback=''))}}">unlock and rollback</a><br/>
<script type="text/javascript">
var bShellScrolling=0
$(document).ready(function(){$('#statement').focus();})

$('#statement').keyup(function(event){ 
  var t=$('#statement');
  var s=t.val()
  var o=$('#output')
  if(s=='\n') t.val('');
  if(s.length>1 && s.substr(s.length-1,1)=='\n' && s.substr(s.length-2,1)!=':' &&
      (s.indexOf(':\n ')<0 || s.substr(s.length-2,1)=='\n')) {
      t.val('');
      $.post("{{=URL(r=request,f='callback',args=app)}}",
       {statement:s},function(data){o.html(o.html()+data).attr('scrollTop',o.attr('scrollHeight'));});
  } else { };
  if(event.keyCode==38){
    var i=s.length
    if(i==0){
        var s=o.find('table:last pre:first').text();
        bShellScrolling=o.find('table').length;
    }else if(bShellScrolling){
        var i=bShellScrolling
        if(i<1){
            return
        }else{
            i--
            var s=o.find('table:nth-child('+(i)+') pre:first').text();
            bShellScrolling=i
        }
    }else if(s.indexOf('\n')<0){
        var oo=o.find('tr:first-child pre:contains("'+s+'")')
        if(oo.length==0){
            return
        }else if(oo.length==1){
            s=oo.text();
        }else{
            sVar=oo.text();
            o.html(o.html()+'<dd>'+s+' ?</dd><dt>'+sVar+'</dt>').attr('scrollTop',o.attr('scrollHeight'))
            return
        }
    }else{
      //multistring expr
      return;
    }
    //  if(s.slice(s.length-1)=='\n'){
    s=s.slice(0,s.length-1)
    //	}
    t.val(s);
  }
  if(bShellScrolling && event.keyCode==40){
    var i=bShellScrolling
    i++
    var s=o.find('table:nth-child('+i+') tr:first-child pre').text();
    if(s){
        s=s.slice(0,s.length-1)
        t.val(s);
        bShellScrolling=i
    }else{
        bShellScrolling=0
        t.val('')
    }
  };
  if(bShellScrolling && (event.keyCode==37 || event.keyCode==39)){
    bShellScrolling=0;
  };
  if(event.keyCode==27){
    bShellScrolling=0;
    t.val('');
  };
});
</script>
</body>
</html>
